package com.fh.shop.api.address.controller;

import com.fh.shop.api.address.biz.IAddressService;
import com.fh.shop.api.address.po.Address;
import com.fh.shop.api.annotation.NeedLogin;
import com.fh.shop.api.common.Constant;
import com.fh.shop.util.ServerResponse;
import com.fh.shop.api.member.vo.MemberVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api/address")
@Api(tags = "收货地址接口")
public class AddressController {
    @Resource(name = "addressService")
    private IAddressService addressService;
    @Autowired
    private HttpServletRequest request;

    @PostMapping
    @NeedLogin
    @ApiOperation("增加收货地址")
    @ApiImplicitParam(name = "x-auth",value = "请求头",required = true,paramType = "header")
    public ServerResponse add(Address address){
        MemberVo memberVo = (MemberVo) request.getAttribute(Constant.MEMBERVO);
        Long memberId = memberVo.getId();
        address.setMemberId(memberId);
        return addressService.add(address);
    }

    @GetMapping
    @NeedLogin
    @ApiOperation("查询收货地址")
    @ApiImplicitParam(name = "x-auth",value = "请求头",required = true,paramType = "header")
    public ServerResponse list(){
        MemberVo memberVo = (MemberVo) request.getAttribute(Constant.MEMBERVO);
        Long memberId = memberVo.getId();
        return addressService.findList(memberId);
    }

    @DeleteMapping
    @NeedLogin
    @ApiOperation("删除收货地址")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "收货地址id",required = true,example = "0"),
            @ApiImplicitParam(name = "x-auth",value = "请求头",required = true,paramType = "header")
    })
    public ServerResponse delete(Long id){
        MemberVo memberVo = (MemberVo) request.getAttribute(Constant.MEMBERVO);
        Long memberId = memberVo.getId();
        return addressService.deleteAddress(id,memberId);
    }

    @GetMapping("{id}")
    @NeedLogin
    @ApiOperation("回填收货地址")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "收货地址id",required = true,example = "0"),
            @ApiImplicitParam(name = "x-auth",value = "请求头",required = true,paramType = "header")
    })
    public ServerResponse find(@PathVariable Long id){
        return addressService.findAddressById(id);
    }

    @PutMapping
    @NeedLogin
    @ApiOperation("修改收货地址")
    @ApiImplicitParam(name = "x-auth",value = "请求头",required = true,paramType = "header")
    public ServerResponse update(@RequestBody Address address){
        MemberVo memberVo = (MemberVo) request.getAttribute(Constant.MEMBERVO);
        Long memberId = memberVo.getId();
        address.setMemberId(memberId);
        return addressService.updateAddress(address);
    }

    @PostMapping("/updateStatus")
    @NeedLogin
    @ApiOperation("修改收货地址状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "收货地址id",required = true,example = "0"),
            @ApiImplicitParam(name = "x-auth",value = "请求头",required = true,paramType = "header")
    })
    public ServerResponse updateStatus(Long id){
        MemberVo memberVo = (MemberVo) request.getAttribute(Constant.MEMBERVO);
        Long memberId = memberVo.getId();
        return addressService.updateStatus(id,memberId);
    }
}
